﻿/******************************************************************************
    myBoard is a port of MacOS X dashboard application for windows.
    Copyright (C) 2007,2008  by Yann ALET
    Email : yann.alet@gmail.com

    myBoard is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    myBoard is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
****************************************************************************/

using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Windows.Data;
using System.Reflection;
using System.IO;
using myBoard.Framework;

namespace Guide.XMLTV
{
    public class Grabber : INotifyPropertyChanged
    {
        // Fields
        private ConfigChannelsList _channels = new ConfigChannelsList();
        private GrabbersEnum _currentGrabber;
        private StringBuilder _header;
        private string _name;
        private List<string> _savedSelectedChannels;

        // Events
        public event PropertyChangedEventHandler PropertyChanged;

        // Methods
        public Grabber(GrabbersEnum grabber)
        {
            this._currentGrabber = grabber;
            switch (grabber)
            {
                case GrabbersEnum.tv_grab_be:
                    this._name = "Belgium and Luxemburg";
                    break;

                case GrabbersEnum.tv_grab_ch_search:
                    this._name = "Switzerland";
                    break;

                case GrabbersEnum.tv_grab_ar:
                    this._name = "Argentina";
                    break;

                case GrabbersEnum.tv_grab_fr:
                    this._name = "France";
                    break;

                case GrabbersEnum.tv_grab_it:
                    this._name = "Italy";
                    break;

                case GrabbersEnum.tv_grab_es:
                    this._name = "Spain";
                    break;

                case GrabbersEnum.tv_grab_uk_rt:
                    this._name = "UK and Ireland";
                    break;

                default:
                    this._name = Enum.GetName(typeof(GrabbersEnum), grabber);
                    break;
            }
            this.BuildChannels(grabber);
        }

        protected void BuildChannels(GrabbersEnum grabber)
        {
            this._header = new StringBuilder();
            Assembly executingAssembly = Assembly.GetExecutingAssembly();
            using (Stream stream = executingAssembly.GetManifestResourceStream(string.Format("{0}.{1}.{2}.conf", executingAssembly.GetName().Name, "External", Enum.GetName(typeof(GrabbersEnum), grabber))))
            {
                StreamReader reader = new StreamReader(stream);
                string str = string.Empty;
                try
                {
                    if (grabber == GrabbersEnum.tv_grab_be)
                    {
                        while (!reader.EndOfStream)
                        {
                            str = reader.ReadLine();
                            if (!(string.IsNullOrEmpty(str) || !str.StartsWith("channel")))
                            {
                                this._channels.Add(new ConfigChannel(str, grabber));
                            }
                            else
                            {
                                this._header.AppendLine(str);
                            }
                        }
                    }
                    else
                    {
                        while (!reader.EndOfStream)
                        {
                            str = reader.ReadLine();
                            if (!string.IsNullOrEmpty(str))
                            {
                                this._channels.Add(new ConfigChannel(str, grabber));
                            }
                        }
                    }
                }
                catch (Exception exception)
                {
                    ExceptionsManager.LogException("myGuide", exception, true);
                }
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged(this, new PropertyChangedEventArgs("Channels"));
                }
            }
        }

        protected List<string> GetSavedSelectedChannels()
        {
            List<string> list = new List<string>();
            string path = Path.Combine(Path.Combine(Controller.SharedProperties.MagnetStorage.StorageDirectory.FullName, ".xmltv"), Enum.GetName(typeof(GrabbersEnum), this._currentGrabber) + ".conf");
            if (File.Exists(path))
            {
                using (StreamReader reader = new StreamReader(path))
                {
                    string str3;
                    if (this._currentGrabber == GrabbersEnum.tv_grab_be)
                    {
                        while (!reader.EndOfStream)
                        {
                            str3 = reader.ReadLine();
                            if (str3.StartsWith("channel"))
                            {
                                list.Add(str3);
                            }
                        }
                        return list;
                    }
                    while (!reader.EndOfStream)
                    {
                        str3 = reader.ReadLine();
                        list.Add(str3);
                    }
                }
            }
            return list;
        }

        internal string GetUserChannelsConfiguration()
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(this._header.ToString());
            foreach (ConfigChannel channel in this._channels)
            {
                if (channel.Selected)
                {
                    builder.AppendLine(channel.ToString());
                }
            }
            return builder.ToString();
        }

        public override string ToString()
        {
            return Enum.GetName(typeof(GrabbersEnum), this._currentGrabber);
        }

        internal void UpdateChannelsSelection()
        {
            this._savedSelectedChannels = this.GetSavedSelectedChannels();
            foreach (ConfigChannel channel in this._channels)
            {
                if (this._savedSelectedChannels.Contains(channel.ChannelOriginalString))
                {
                    channel.Selected = true;
                }
            }
        }

        protected bool WasSelected(string channelLine)
        {
            foreach (string str in this._savedSelectedChannels)
            {
                if (str == channelLine)
                {
                    return true;
                }
            }
            return false;
        }

        // Properties
        public ConfigChannelsList Channels
        {
            get
            {
                return this._channels;
            }
        }

        public string Name
        {
            get
            {
                return this._name;
            }
            set
            {
                this._name = value;
            }
        }

    }

    internal class ActiveGrabberValueConvertor : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            switch ((GrabbersEnum)value)
            {
                case GrabbersEnum.tv_grab_fr:
                    return "France";

                default:
                    return "???";
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
